LOGO Interactive

How to Write LOGO Interactive Programs

What do we mean by “interactive”? An interactive program is one in which the user can interact with the program by providing inputs to the program via a keyboard or mouse. 

These user’s inputs can change the flow of the program to enable it to decide what the program will do next.

An example of an interactive program is Video games. The game programs need to be interactive so that the player can use the mouse or keyboard to avoid a speeding car (as an example of an obstacle avoidance game.)

What We Have Done So Far

So far, all programs we have run are non-interactive. What that means is that when you run the program over and over again, it produces the same result.

On the other hand, an interactive program asks the user to do something like provide input, click the OK button, or type something before it can proceed further. The program then waits for the user to respond to the program’s request to take the requested action. When the user responds with an action through a keyboard or mouse click, the program stores the user’s input in a variable.  The program then checks the variable to determine what to do next.

In this chapter, we will use the LOGO programming language to write some simple math games.

A Few LOGO Interactive Commands

The following are a few LOGO commands we will use to create interactive programs.

READWORD Command

The programs can be made interactive by using LOGO’s READWORD command.  The READWORD command reads the user’s input and stores it in a variable. Then the program uses that value stored in the variable and proceeds based on the variable’s value.

READWORD Details are at this link.

READWORD Command Usage Example:

make “game READWORD

In the statement example above, the make command (See LOGO Arithmetic Commands page) the statement

make “game READWORD

defines a variable named “game”.

When you write the above statement and hit the enter key, a small dialog box appears on the screen. The READWORD command asks LOGO to wait for the user to enter some text in the dialog box.  The dialog box input mode prompts the user to type something.  After the user types a word, let us say, “soccer” in the dialog box and hits the OK button, the program stores the word “soccer” in the variable “game”. See example below

Below is the screen print of the READWORD usage example.

Interactive LOGO. Use of READWORD command to enter text in the dialog box.
LOGO READWORD COMMAND USAGE

To see what the READWORD command stored in the variable “game”, we type the statement

SHOW :game

This statement prints the value stored in the variable “game”.  Since the word “soccer” is stored in the variable “game”, the program prints the word “soccer”.  This is shown in the next figure.

LOGO Interactive SHOW command prints the word entered by the user in the dialog box in response to the READWORD command.
SHOW COMMAND READS THE TEXT ENTERED BY THE USER

More information of READWORD command is at:

<a href=”https://fmslogo.sourceforge.io/manual/command-readword.html“>

Adder Program Using READWORD Command

Now that we understand how to write programs that let the user interact with the program, let’s write a simple program to add two numbers/ These numbers are entered by the user into the program. Open a Notepad program on your computer and type the following text that is the statements of the Adder program.

PROGRAM EXAMPLE: ADDER USING READWORD COMMAND
; Simple Adder program.
; Add two numbers "number1" and "number2".
PRINT [Enter first number]
; Define a variable "number1"
MAKE "number1 READWORD
; READWORD command stores the number entered 
; by the user in the variable "number1".
; Define another variable "number2"
PRINT [Enter the second number]
MAKE "number2 READWORD
; READWORD command stores the number entered 
: by the user in the variable "number2".
PRINT [The sum of number1 and number2 =] 
PRINT :number1 + :number2 

Now start FMSLOGO.  Create the ADDER procedure by typing

TO ADDER

in the LOGO window command line. Copy and paste the above ADDER program into the window that opens.  Click on the END button at the bottom of the window.  Now your ADDER Procedure is ready to run. You can add any two numbers that you input into the program.

To run your ADDER program, type ADDER in the FMSLOGO command line to call the ADDER procedure that you just wrote.  The program will open a dialog box and ask you to enter the first number.  After you enter your first number and click on the OK button in the dialog box, the program will open another dialog box and ask you to enter the second number to be added. Once you enter the second number and click on the OK button, the program will show you the result of the addition. The following image shows the result of the program.

Challenge:  Write a program to multiply two numbers.

Here is the program’s output.

Interactive LOGO A simple Adder using the READWORD command.
INTERACTIVE LOGO A SIMPLE ADDER PROGRAM. Output Using READWORD Command

Adder – Math Game

Now let us add some interesting features to our ADDER program.  This program will provide five addition problems and ask the user to type the answer using the LOGO ReadWord command.  The program will check the user’s answer for correctness.  If the answer the user inputs via the keyboard is correct, the program responds by printing “CORRECT ANSWER!”.  If the answer input by the user is not correct, the program prints “No, The correct answer is” and prints the correct answer. 

After the program has exhausted five problems, it asks the user to input a response about whether the user would like to continue or quit the game by typing ‘1’ for QUIT and ‘2’ for CONTINUE.  

Copy and run this program on your computer and play this game.

PROGRAM EXAMPLE: ADDER MATH GAME
TO AddQuiz
; This program gives you an addition problem to add two numbers
; and asks the player to type the answer. 
; The program gives five problems one at a time.
; The program uses X as the looping variable foe the WHILE loop to 
; count the problems from 1 to 5 to present the 5 problems to the player.
MAKE "X 1
WHILE [:X < 5] [MAKE "X :X + 1 
MAKE "NUMBER1 random 10 ; LOGO selects a random number between 0 and 10 and stores in variable "NUMBER1".
MAKE "NUMBER2 random 10 ; LOGO selects a random number between 0 and 10 and stores it in variable "NUMBER2".
MAKE "SUM :NUMBER1+ :NUMBER2 ; LOGO computes the answer and stores the answer in variable "SUM".
; LOGO prompts the player to to type the answer. 
PRINT (sentence "what "is :NUMBER1 "+ :NUMBER2 "?)  
MAKE "ANSWER readWord ; LOGO stores the value typed by the player in variable "ANSWER".
; LOGO compares the value computed by the program stored in variable "SUM"
; to the value input by the player stored in the variable "ANSWER".
; If the value computed by the program (TOTAL) is equal to 
; the value guessed by the player (ANSWER), PRINT "CORRECT ANSWER!". 
; If the two values do not match, the program prints "NO, CORRECT ANSWER IS " 
IFELSE :SUM = :answer [PRINT [CORRECT ANSWER!] ] [ print (sentence "No, "THE "CORRECT "ANSWER "IS :total) ] ]
PRINT (SENTENCE "DO "YOU "WANT "TO "PLAY "AGAIN "TYPE "1 "FOR "QUIT "OR "2 "TO "CONTINUE?)
MAKE "RESPONSE readWord ; Player input is stored in the variable "RESPONSE".
IFELSE :RESPONSE = "1 [PRINT [QUITING]] [AddQuiz] 
; If the player wants to continue playing by typing "2", the program makes 
; a RECURSION CALL to the "AddQuiz" program and starts the WHILE loop again.
; if the player wants to quit the game by typing "1", the program prints "QUITING".

end

The following is program’s intermediate output.

Interactive LOGO ADDER game using READWORD Command.
MATH GAME USING READWORD COMMAND

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights